package com.soul.dj.ui.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.soul.dj.R;
import com.soul.dj.ui.fragment.IndexFragment;
import com.soul.dj.ui.fragment.MineFragment;
import com.soul.dj.util.Const;

import java.io.File;

public class MainActivity extends BaseActivity {


    public Fragment[] fragments;
    private int index;
    // 当前fragment的index
    private int currentTabIndex;
    private ImageView searchImage,mineImage;
    private TextView searchTxt,mineTxt;

    @Override
    protected void initView() {
        setContentView(R.layout.main_activity);

        File filePath = new File(Const.IMAGE_PATH);
        if(!filePath.exists())
        {
            filePath.mkdirs();
        }

        findViewById(R.id.searchLayout).setOnClickListener(this);
        findViewById(R.id.mineLayout).setOnClickListener(this);

        searchImage=(ImageView) findViewById(R.id.searchImage);
        searchTxt=(TextView) findViewById(R.id.searchTxt);
        mineImage=(ImageView) findViewById(R.id.mineImage);
        mineTxt=(TextView) findViewById(R.id.mineTxt);

        fragments = new Fragment[2];
        fragments[0] = new IndexFragment();

        FragmentManager fm = getSupportFragmentManager();
        fm.beginTransaction().add(R.id.fragmentContainer, fragments[0])
                .show(fragments[0]).commit();

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        onTabClicked(v);
    }

    /**
     * button点击事件
     *
     * @param view
     */
    public void onTabClicked(View view) {
        switch (view.getId()) {
            case R.id.searchLayout:
                index = 0;
                break;
            case R.id.mineLayout:
                if (fragments[1] == null) {
                    fragments[1] = new MineFragment();
                }
                index = 1;
                break;

        }
        switch (index) {
            case 0:
                searchImage.setBackgroundResource(R.drawable.task_blue);
                searchTxt.setTextColor(getResources().getColor(R.color.main_menu_txt_blue));
                mineImage.setBackgroundResource(R.drawable.my);
                mineTxt.setTextColor(getResources().getColor(R.color.main_menu_txt));
                break;

            case 1:
                searchImage.setBackgroundResource(R.drawable.task);
                searchTxt.setTextColor(getResources().getColor(R.color.main_menu_txt));
                mineImage.setBackgroundResource(R.drawable.my_blue);
                mineTxt.setTextColor(getResources().getColor(R.color.main_menu_txt_blue));
                break;

        }
        if (currentTabIndex != index) {
            FragmentTransaction trx = getSupportFragmentManager()
                    .beginTransaction();
            trx.hide(fragments[currentTabIndex]);
            if (!fragments[index].isAdded()) {
                trx.add(R.id.fragmentContainer, fragments[index]);
            }
            trx.show(fragments[index]).commit();
        }
        currentTabIndex = index;
    }

    public void setCurrentFirstFragment() {
        if (currentTabIndex != 0) {
            FragmentTransaction trx = getSupportFragmentManager()
                    .beginTransaction();
            trx.hide(fragments[currentTabIndex]);
            if (!fragments[0].isAdded()) {
                trx.add(R.id.fragmentContainer, fragments[0]);
            }
            trx.show(fragments[0]).commitAllowingStateLoss();
        }
        searchImage.setBackgroundResource(R.drawable.task_blue);
        searchTxt.setTextColor(getResources().getColor(R.color.main_menu_txt_blue));
        mineImage.setBackgroundResource(R.drawable.my);
        mineTxt.setTextColor(getResources().getColor(R.color.main_menu_txt));
        currentTabIndex = 0;
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
//        mLocationClient.stop();
    }
}



